import eventBus from "@/views/personnelRegulate/rewardManagement/smartSalary/salarySheet/eventBus/eventBus";

export default function initXc(connection, that) {
  // 生成工资表
  connection.on("GenerateWages", async e => {
    console.log("-------------------------------接收到消息------------------------------");
    const message = "生成工资表";
    await handleFunc(e, "GenerateWages", that).then(data => {
      successPop(that, data, message);
      eventBus.$emit("GenerateWages", true);
    }).catch(err => {
      catchPop(that, err);
    });
  });
  // 重新计算
  connection.on("RegenerateWages", async e => {
    console.log("-------------------------------接收到消息------------------------------");
    const message = "重新计算工资表";
    await handleFunc(e, "RegenerateWages", that).then(data => {
      successPop(that, data, message);
      if (that.$route.name == name) that.reload();
    }).catch(err => {
      catchPop(that, err);
    });
  });
  // 导入
  connection.on("WagesImport", async e => {
    console.log("-------------------------------接收到消息------------------------------");
    const message = "导入工资表";
    await handleFunc(e, "WagesImport", that).then(data => {
      successPop(that, data, message);
      if (that.$route.name == name) that.reload();
    }).catch(err => {
      catchPop(that, err);
    });
  });
}

async function handleFunc(e, key, that) {
  return new Promise((resolve, reject) => {
    console.log(that, JSON.parse(e), key);
    const data = JSON.parse(e);
    const { Status } = data;
    that.$store.commit("setOperationLoading", { key, val: false });
    if (Status == 200) {
      const { id, subid } = data;
      resolve({ id, subId: subid });
    } else {
      reject(data.message);
      return false;
    }
  });
}
const title = "您有一条新消息,点击查看";
const name = "salaryDetails";

function successPop(that, data, message) {
  that.$notify({
    title,
    onClick: function() {
      this.close();
      console.log(that.$route.name, name);
      if (that.$route.name == name) console.log('点击'); //that.reload();
      else
        that.$router.push({
          name,
          query: { ...data, random: Math.random() }
        });
    },
    duration: 0,
    message: message + "成功",
    dangerouslyUseHTMLString: true,
    customClass: "infoNotify",
    type: "success"
  });
}
function catchPop(that, message) {
  that.$notify({
    title,
    onClick: function() {
      this.close();
    },
    duration: 0,
    message,
    dangerouslyUseHTMLString: true,
    customClass: "infoNotify",
    type: "error"
  });
}
